<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/Plugins.class.php, $Author:  $ */

// Lets start the plugin
$oPlugin = new phpFoXPlugins();

$_PLUGIN['header'] = array();
$_PLUGIN['botttom_links'] = array();
$_PLUGIN['main_links'] = array();
$_PLUGIN['admin_links'] = array();
$phpfoxPlugins = array();
$aPhpFoxHooks = array();
$aCalledHooks = array();

if (!defined('PHPFOX_NO_PLUGINS'))
{
	$dir = App::getParam('path') . "plugins/";
	if (is_dir($dir))
	{
		if ($dh = opendir($dir))
		{
			while (($file = readdir($dh)) !== false)
			{
				if ( file_exists($dir . $file ."/phpfox_functions.php"))
	       		{
	       			$phpfoxPlugins[] = $file;
	        		require_once(App::getParam('path') . 'plugins/'. $file .'/phpfox_functions.php');
	        	}
	    	}
	      	closedir($dh);
	   	}
	}
	
	$oCache = App::newObject('Cache');
	$oCache->setFile('hooks.php');	
	
	$bIsCached = false;
	if (!App::getParam('bHooksLive'))
	{
		$bIsCached = false;
	}
	else 
	{
		$bIsCached = $oCache->isCached();
	}	
	
	if (!$bIsCached)
	{
		foreach ($phpfoxPlugins as $sFile) 
		{
	        if (is_dir(App::getParam('path') . 'plugins/'. $sFile .'/hooks/'))
	        {
	        	$rHooks = opendir(App::getParam('path') . 'plugins/'. $sFile .'/hooks/');
	        	while (($sHook = readdir($rHooks)) !== false)
				{
					if (substr($sHook, -4) != '.php')
					{
						continue;
					}
					$sHookContent = file_get_contents(App::getParam('path') . 'plugins/'. $sFile .'/hooks/' . $sHook);
					if (substr($sHookContent, 0, 5) == '<?php')
					{
						$sHookContent = substr_replace($sHookContent, '', 0, 5);
					}
					if (substr($sHookContent, 0, 2) == '<?')
					{
						$sHookContent = substr_replace($sHookContent, '', 0, 2);
					}
					if (substr($sHookContent, -2) == '?>')
					{
						$sHookContent = substr_replace($sHookContent, '', -2);
					}
					$sHookContent = trim($sHookContent);
					$sHookVarName = str_replace('.php', '', $sHook);
					
					if (isset($aPhpFoxHooks[$sHookVarName]))
					{
						$sHookContent = '' . $aPhpFoxHooks[$sHookVarName] . $sHookContent . "\n\t";
					}
					
					$aPhpFoxHooks[$sHookVarName] = $sHookContent . "\n\t";
				}
	        }  		
		}
		$oCache->save($aPhpFoxHooks);
	}
	$aPhpFoxHooks = $oCache->doRequire();
}

/**
 * Plugin application class.
 *
 * @package core
 *
 * @version $Id: Plugins.class.php 481 2008-08-13 06:14:56Z phpfoxsv $
 */
class phpFoXPlugins
{
 	/**
 	* Start the class
 	*/
	function phpFoXPlugins()
	{
	}

	/**
	* Creates phpfox $_GET
	*
	* @return		string		$_GET
	*/
	function _get($name)
	{
		global $_CONF;

		if ( !isset($_GET[$_CONF['get_path_var']]) )
		{
			return;
		}

		$do = strip_tags($_GET[$_CONF['get_path_var']]);
		$link = explode($name."_",$do);
		if ( !isset($link[1]) )
		{
			return;
		}
		$link_main = explode("/",$link[1]);
		if ( isset($link_main[0]) )
		{
			return $link_main[0];
		}
	}

	/**
	* Creates sub links
	*
	* @return 		string		returns content
	*/
	function create_sub_links($name)
	{
		global $_PLUGIN;

		if ( !isset($_PLUGIN[$name]) )
		{
			return false;
		}
		$plugin_bottom_links = array();
		$newbLink = "";
		$count = 0;
		foreach($_PLUGIN[$name] as $link => $value)
		{
			$count++;
			$newbLink['count'] = $count;
			$newbLink['name'] = $value;
			$newbLink['link'] = $link;

			$parse = explode("|",$value);
			if ( isset($parse[0]) AND isset($parse[1]) )
			{
				$parse = explode("|",$value);
				$newbLink['name'] = $parse[0];
				$newbLink['extra'] = $parse[1];
			}

			$plugin_bottom_links[] = $newbLink;
		}
		return $plugin_bottom_links;
	}

	/**
	* Creates extra content
	*
	* @return 	string		content
	*/
	function create_content($name)
	{
		global $_PLUGIN;
		if ( !isset($_PLUGIN['content'][$name]) )
		{
			return false;
		}
		$header = "";
		foreach($_PLUGIN['content'][$name] as $value)
		{
			$header .= $value."\n";
		}

		return $header;
	}

	/**
	* Creates extra <head>
	*
	* @return 	string		content
	*/
	function create_header()
	{
		global $_PLUGIN;
		$header = "";
		foreach($_PLUGIN['header'] as $value)
		{
			$header .= $value."\n";
		}

		return $header;
	}

	/**
	* Creates bottom links
	*
	* @return		string 	links
	*/
	function create_bottom_links()
	{
		global $_PLUGIN;

		$plugin_bottom_links = array();
		$newbLink = "";
		$count = 0;
		foreach($_PLUGIN['botttom_links'] as $link => $value)
		{
			$count++;
			$newbLink['count'] = $count;
			$newbLink['name'] = $value;
			$newbLink['link'] = $link;
			$plugin_bottom_links[] = $newbLink;
		}
		return $plugin_bottom_links;
	}

	/**
	* Creates main links
	*
	* @return		string 	links
	*/
	function create_main_links()
	{
		global $_PLUGIN;

		$plugin_main_links = array();
		$newbLink = "";
		$count = 0;
		foreach($_PLUGIN['main_links'] as $link => $value)
		{
			$count++;
			$newbLink['count'] = $count;
			$newbLink['name'] = $value;
			$newbLink['link'] = $link;
			$plugin_main_links[] = $newbLink;
		}
		return $plugin_main_links;
	}

	/**
	* Creates admin links
	*
	* @return		string 	links
	*/
	function create_admin_links()
	{
		global $_PLUGIN;

		$plugin_main_links = array();
		$newbLink = "";
		$count = 0;
		foreach($_PLUGIN['admin_links'] as $link => $value)
		{
			$count++;
			$newbLink['count'] = $count;
			$newbLink['name'] = $value;
			$newbLink['link'] = $link;
			$plugin_main_links[] = $newbLink;
		}
		return $plugin_main_links;
	}
	
	function call($sHook)
	{
		global $aPhpFoxHooks;
		global $aCalledHooks;
		
		if (App::getParam('bHooksDisplay') && !strpos(App::getPage(),'ublic.rss'))
		{
	    	echo "<!-- phpFox Hook: " . $sHook . " -->\n";
	    }		

		if ( defined("benchMark") && benchMark && App::getParam('debug') )
		{
			$aCalledHooks[] = $sHook;
		}		
    
		if (isset($aPhpFoxHooks[$sHook]))
		{
			return $aPhpFoxHooks[$sHook];
		}		
		return false;
	}

 	/**
 	* Call the function for each plugin
 	*
 	* @param	string			Name of the function
 	* @param	string/array	Param for the plugin
 	*/
	function call_phpfox_function($function, $params = '')
	{
	 	global $phpfoxPlugins;

	 	$item = '';
	 	foreach($phpfoxPlugins as $iKey => $plugin)
		{
	 		$function_name = $plugin.'_'.$function;

 	 		if ( function_exists($function_name) )
			{
				$item = $function_name($params); // Call $function
				/*
				$item[$function][$iKey] = $function_name($params); // Call $function
				if ( empty($item[$function][$iKey]) )
				{
					unset($item[$function][$iKey]);
				}
				*/
			}
		}

		return $item;
		// return (isset($item[$function]) ? $item[$function] : false);
	}

 	/**
 	* Display threads
 	*
 	*/
	function phpfoxForumThreads()
    {
		return $this->call_phpfox_function('forum_threads');
    }

 	/**
 	* Display topics
 	*
 	*/
    function phpfoxForumTopics()
    {
    	return $this->call_phpfox_function('forum_topics');
    }

 	/**
 	* Create a user account
 	*
 	* @param	array		_POST data
 	*/
    function phpfoxCreateUser($oAccount)
    {
		$this->call_phpfox_function('create_user',$oAccount);
    }

 	/**
 	* Change the users password
 	*
 	* @param	array		_POST data
 	*/
    function phpfoxChangePassword($oAccount)
    {
		$this->call_phpfox_function('update_password',$oAccount);
    }

 	/**
 	* Login the user
 	*
 	* @param	array		_POST data
 	*/
	function phpfoxLogin( $oAccount )
	{
		$this->call_phpfox_function('login',$oAccount);
	}

 	/**
 	* Change the CSS
 	*
 	* @param	array		_POST data
 	*/
    function phpfoxCss($oAccount)
    {
		$this->call_phpfox_function('change_theme',$oAccount);
    }

 	/**
 	* Logout the user
 	*
 	* @param	array		_POST data
 	*/
    function phpfoxLogout($oAccount)
    {
		$this->call_phpfox_function('logout_user',$oAccount);
    }

 	/**
 	* Update the account
 	*
 	* @param	array		_POST data
 	*/
    function phpfoxUpdateAccount($oAccount)
    {
		$this->call_phpfox_function('update_account',$oAccount);
    }
}

?>